#include <stdio.h>
#include <stdlib.h>
#include <iostream>

#define MAXSIZE 1000 
#define INPUT_FILEPATH  "C:\\Users"

struct node 
{
    uint64_t add ;
    int indegree ;
    int outdegree ;
    struct next_node * next ;
};

struct next_node 
{
    uint16_t des ;
    struct next_node * next ;
};

int arr_size = 0 ;
uint64_t src ;
uint64_t des ;
int copy_size ;

int main()
{
    struct node * sto = (struct node *) malloc (1000 * sizeof(struct node)) ;
    FILE * inputFile = fopen(INPUT_FILEPATH , "a+") ;
    if (inputFile == NULL)
    {
        printf("文件打开失败");
        return 1 ;
    }
    else
    {
        while (fscanf(inputFile, "%llu %llu %d", &src, &des, &copy_size) == 3)
        {
            create_node(src , des , sto) ;
        }
    }

}

int create_node (uint64_t src , uint64_t des , struct node * sto)
{
    int i = 0 , j = 0 ;
    struct node * p ;
    int x ;
    
    //先对src进行监测
    x = find_if_same (src , sto) ;
    if (x == -1)
    {
        x = arr_size ;
        p = (sto + x) ;
        create_new_node (src , p) ;
        p -> outdegree ++ ;
        arr_size ++ ;
        //补充null
    }
    else
    {
        p = sto + x ;
        p -> outdegree ++ ;
    }
    i = x ;

    //然后对des进行监测
    x = find_if_same (des , sto) ;
    if (x == -1)
    {
        x = arr_size ;
        p = sto + x ;
        create_new_node (des , p) ;
        p -> indegree ++ ;
        arr_size ++ ;
    }
    else
    {
        p = sto + x ;
        p -> indegree ++ ;
    }
    j = x ;

    //录入边信息
    if ((sto + i) -> next == NULL)
    {
        (sto + i) -> next == (sto + j) ;
    }
    else
    {
        struct next_node * p1 = (sto + i) -> next ;
        while (p1 -> next != NULL)
        {
            p1 = p1 -> next ;
        }
        p1 -> next = (sto + j) ;
    }
    return 0 ;
}

//返回-1 没有找到 返回非-1 找到
int find_if_same (uint64_t target , struct node * sto)
{
    int i = 0 ;
    int j = arr_size - 1 ;
    while (i <= j)
    {
        if (target == (sto + i)->add )
        {
            return i ;
        }
        i++;
    }
    return -1 ;
}

//在sto【x】上创建新节点
int create_new_node(uint64_t target_add , struct node * p)
{
    p -> add = target_add ;
    p -> indegree = 0 ;
    p -> outdegree = 0 ;
    p -> next = NULL ;
    return 0 ;
}

int print_nodes(struct node * sto)
{
    int i = 0 ;
    while (i < arr_size)
    {
        if ((sto + i)->indegree == 0)
        {
            if ((sto + i)->next == NULL )
            {
                printf("ERROR1 , 入度为0的节点后没有节点") ;
                return 0 ;
            }
            else
            {
                printf("0x%016llX\n" , (sto + i)->add) ;
                show_node (sto , i , 1);
            }
        }
        i++ ;
    }
    
}

int show_node (struct next_node * sto , int i , int depth)
{
    int j = 0 ;
    while (j < depth)
    {
        printf("********************");
        j++ ;
    }
    
}














